package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.core.list.MBBTable;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.ColumnInformation;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class DiskCacheAdapter extends TableDataAdapter
{
  private transient MBBTable mbb_table = null;

  public DiskCacheAdapter(DatabaseConnection paramDatabaseConnection, String paramString, int paramInt)
  {
    super(paramDatabaseConnection, paramString, paramInt);
  }

  protected void initRowValueStatus()
  {
    try
    {
      checkInColumn();
      this.mbb_table = new MBBTable(getColumnCount(), shortcutGetRowCount());
    }
    catch (TableDataException localTableDataException)
    {
      localTableDataException.printStackTrace();
    }
  }

  protected synchronized boolean hasRow(int paramInt)
    throws TableDataException
  {
    if ((this.mbb_table != null) && (this.mbb_table.columnCount() > 0) && (this.mbb_table.rowCount() > paramInt))
      return true;
    if (this.isCheckRowOver)
      return false;
    initConnectionAndResultAndCheckInColumns();
    if (this.connection == null)
      return false;
    Dialect localDialect = DialectFactory.generateDialect(this.connection);
    int i = getColumnCount();
    String str1 = this.database.getOriginalCharsetName();
    String str2 = this.database.getNewCharsetName();
    int j = this.mbb_table.rowCount();
    try
    {
      int k;
      do
      {
        if (!(this.resultSet.next()))
          break label184;
        for (k = 0; k < i; ++k)
          try
          {
            this.mbb_table.addValueAt(k, DataUtils.getResultSetObject(this.resultSet, this.columnInformations[k].getColumnType(), k + 1, str1, str2, localDialect));
          }
          catch (SQLException localSQLException)
          {
            this.mbb_table.addValueAt(k, null);
          }
      }
      while (++j <= paramInt);
      return true;
      label184: this.isCheckRowOver = true;
      releaseConnection();
    }
    catch (Exception localException1)
    {
      try
      {
        releaseConnection();
      }
      catch (Exception localException2)
      {
        FRContext.getLogger().log(Level.WARNING, "Error happens while releaseConnection");
      }
      this.isCheckRowOver = true;
      throw new TableDataException(localException1.getMessage() + "\nQuery is:" + this.sql + "\nrowIndex to get is:" + paramInt, localException1);
    }
    return (j > paramInt);
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return ((hasRow(paramInt1)) ? this.mbb_table.getValueAt(paramInt2, paramInt1) : null);
  }

  public int getRowCount()
    throws TableDataException
  {
    hasRow(2147483647);
    return this.mbb_table.rowCount();
  }

  protected void release()
    throws SQLException
  {
    if (this.mbb_table != null)
      this.mbb_table.clear();
    super.release();
  }
}